﻿using Microsoft.IdentityModel.Tokens;
using Prism.Commands;
using Prism.Services.Dialogs;
using SmartPlant.Core;
using SmartPlant.Core.Mvvm;
using SmartPlant.Model.Biz;
using SmartPlant.Services.Interfaces;
using SmartPlant.WinCore.Helper;
using System.Windows.Input;

namespace SmartPlant.Modules.Base.ViewModels
{
    public class UserEditViewModel : DialogViewModelBase, IDialogAware
    {
        private IntoolsUser IntoolsUser { get; set; }
        private DelegateCommand<object> _okDialogCommand;
        private IOAuthService _authService;
        public UserEditViewModel(IDialogService dialogService, IOAuthService oAuthService) : base(dialogService)
        {
            Title = "编辑用户";
            _authService = oAuthService;
        }
        public override async void OnDialogOpened(IDialogParameters parameters)
        {
            base.OnDialogOpened(parameters);
            UserId = parameters.GetValue<string>("userId");
            if (!UserId.IsNullOrEmpty())
                IntoolsUser = await _authService.GetUserById(UserId);
            if (IntoolsUser != null)
            {
                InUserName = IntoolsUser.InUserName;
                Pwd = IntoolsUser.UserPassword;
                ConfirmPwd = IntoolsUser.UserPassword;
                IntoolsUserNote = IntoolsUser.IntoolsUserNote;
            }
            else
            {
                IntoolsUser = new IntoolsUser();
            }
        }
        private string _userID;
        public string UserId
        {
            get { return _userID; }
            set { SetProperty(ref _userID, value); }
        }

        private string _inUserName;
        public string InUserName
        {
            get { return _inUserName; }
            set { SetProperty(ref _inUserName, value); }
        }


        private string _pwd;
        public string Pwd
        {
            get { return _pwd; }
            set { SetProperty(ref _pwd, value); }
        }

        private string _confirmPwd;
        public string ConfirmPwd
        {
            get { return _confirmPwd; }
            set { SetProperty(ref _confirmPwd, value); }
        }

        private string _intoolsUserNote;
        public string IntoolsUserNote
        {
            get { return _intoolsUserNote; }
            set { SetProperty(ref _intoolsUserNote, value); }
        }

        public ICommand OkDialogCommand
        {
            get
            {
                if (_okDialogCommand != null) return _okDialogCommand;
                _okDialogCommand = new DelegateCommand<object>(onOkDialogCommand);
                return _okDialogCommand;
            }
        }

        private async void onOkDialogCommand(object parameter)
        {
            if (InUserName.IsNullOrEmpty())
            {
                Dialog.ShowAltert("您尚未设置用户名。");
                return;
            }
            if (Pwd.IsNullOrEmpty())
            {
                Dialog.ShowAltert("您尚未设置用户密码。");
                return;
            }
            if (!Pwd.Equals(ConfirmPwd, StringComparison.CurrentCultureIgnoreCase))
            {
                Dialog.ShowAltert("您设置的用户密码与确认密码不一致。");
                return;
            }

            try
            {
                IntoolsUser.InUserName = InUserName.ToUpper();//转换为大写
                IntoolsUser.UserPassword = Pwd.ToUpper();//转换为大写
                IntoolsUser.IntoolsUserNote = IntoolsUserNote;

                bool? r = false;
                if (UserId.IsNullOrEmpty())
                {
                    r = await _authService.CreateUser(IntoolsUser);
                }
                else
                {
                    r = await _authService.UpdateUser(IntoolsUser);
                }
                if (r.HasValue && r.Value)
                {
                    Dialog.ShowAltert("编辑用户信息成功！！！");
                    RaiseRequestClose(new DialogResult(ButtonResult.OK));
                }
                else
                {
                    RaiseRequestClose(new DialogResult(ButtonResult.None));
                }
            }
            catch (Exception x)
            {
                var xMessage = $"编辑用户信息失败：{x.Message}！！！";
                LoggerHelper.Current.Error(xMessage, x);
                Dialog.ShowAltert(xMessage);
            }
        }
    }
}
